/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.edu.web.userextra;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.config.Global;
import com.idenger.common.persistence.Page;
import com.idenger.common.web.BaseController;
import com.idenger.common.utils.StringUtils;
import com.idenger.modules.edu.entity.userextra.EduUserExtra;
import com.idenger.modules.edu.service.userextra.EduUserExtraService;

/**
 * 用户额外信息Controller
 * @author 兰正军
 * @version 2017-12-07
 */
@Controller
@RequestMapping(value = "${adminPath}/edu/userextra/eduUserExtra")
public class EduUserExtraController extends BaseController {

	@Autowired
	private EduUserExtraService eduUserExtraService;
	
	@ModelAttribute
	public EduUserExtra get(@RequestParam(required=false) String id) {
		EduUserExtra entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eduUserExtraService.get(id);
		}
		if (entity == null){
			entity = new EduUserExtra();
		}
		return entity;
	}
	
	@RequiresPermissions("edu:userextra:eduUserExtra:view")
	@RequestMapping(value = {"list", ""})
	public String list(EduUserExtra eduUserExtra, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EduUserExtra> page = eduUserExtraService.findPage(new Page<EduUserExtra>(request, response), eduUserExtra); 
		model.addAttribute("page", page);
		return "modules/edu/userextra/eduUserExtraList";
	}

	@RequiresPermissions("edu:userextra:eduUserExtra:view")
	@RequestMapping(value = "form")
	public String form(EduUserExtra eduUserExtra, Model model) {
		model.addAttribute("eduUserExtra", eduUserExtra);
		return "modules/edu/userextra/eduUserExtraForm";
	}

	@RequiresPermissions("edu:userextra:eduUserExtra:edit")
	@RequestMapping(value = "save")
	public String save(EduUserExtra eduUserExtra, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eduUserExtra)){
			return form(eduUserExtra, model);
		}
		eduUserExtraService.save(eduUserExtra);
		addMessage(redirectAttributes, "保存用户额外信息成功");
		return "redirect:"+Global.getAdminPath()+"/edu/userextra/eduUserExtra/?repage";
	}
	
	@RequiresPermissions("edu:userextra:eduUserExtra:edit")
	@RequestMapping(value = "delete")
	public String delete(EduUserExtra eduUserExtra, RedirectAttributes redirectAttributes) {
		eduUserExtraService.delete(eduUserExtra);
		addMessage(redirectAttributes, "删除用户额外信息成功");
		return "redirect:"+Global.getAdminPath()+"/edu/userextra/eduUserExtra/?repage";
	}

}